
#include "stdafx.h"
#include "LabelTools.h"
#include "ocafheader.h"
#include <TDataStd_Point.hxx>
#include <TDataStd_Geometry.hxx>
#include <TDataStd_Shape.hxx>
#include <TDataStd_Integer.hxx>
#include <TDataStd_Position.hxx>
#include <TCollection_ExtendedString.hxx>
#include <Standard_TypeDef.hxx>
#include <TDataStd_IntegerArray.hxx>
#include <TColgp_HArray1OfPnt.hxx>
LabelTools::LabelTools()
{

}

bool LabelTools::GetChild(const TDF_Label& Label,CString& value, TDF_Label& subLabel)
{
	Handle(TDataStd_Name)TSR;

	if (!Label.FindAttribute(TDataStd_Name::GetID(), TSR )) 
		return false;
	TCollection_ExtendedString sz = TSR->Get();
	value = (wchar_t*)(sz.ToExtString());
	
	subLabel = TSR->Label();
	return true;
}

void LabelTools::SetChild(TDF_Label& Label, UINT nIndex, Standard_Real value)
{
	TDataStd_Real::Set(Label.FindChild(nIndex), value);
}
bool LabelTools::GetChild(const TDF_Label& Label,UINT nIndex, Standard_Real& value, TDF_Label& subLabel)
{
	subLabel = Label.FindChild(nIndex, Standard_False);
	if(subLabel.IsNull())
		return false;

	Handle(TDataStd_Real) TSR;
	if (!subLabel.FindAttribute(TDataStd_Real::GetID(), TSR )) 
		return false;

	value = TSR->Get();
	return true;
}


void LabelTools::SetChild(TDF_Label& Label, UINT nFirstIndex, const gp_Pnt& point)
{
	TDataStd_Position::Set(Label.FindChild(nFirstIndex), point);
}
bool LabelTools::GetChild(const TDF_Label& Label,UINT nFirstIndex, gp_Pnt& point, TDF_Label& subLabel)
{

	subLabel = Label.FindChild(nFirstIndex, Standard_False);
	if(subLabel.IsNull())
		return false;

	if(TDataStd_Position::Get(subLabel, point))
		return true;

	return false;
}

void LabelTools::SetChild(TDF_Label& Label, UINT nFirstIndex, const gp_Dir& dir)
{
	Standard_Real vx,vy,vz;
	dir.Coord(vx, vy, vz);
	gp_Pnt point(vx,vy,vz);
	
	SetChild(Label, nFirstIndex, point);
}

bool LabelTools::GetChild(const TDF_Label& Label,UINT nFirstIndex, gp_Dir& dir, TDF_Label& subLabel)
{
	gp_Pnt point;
	
	if(GetChild(Label, nFirstIndex, point,subLabel))
	{
		dir.SetCoord(point.X(), point.Y(), point.Z());
		return true;
	}

	return false;
}

void LabelTools::SetChild(TDF_Label& Label, UINT nIndex, const TopoDS_Shape& value)
{
	TDataStd_Shape::Set(Label.FindChild(nIndex), value);
}

bool LabelTools::GetChild(const TDF_Label& Label,UINT nIndex, TopoDS_Shape& value, TDF_Label& subLabel)
{
	subLabel = Label.FindChild(nIndex, Standard_False);
	if(subLabel.IsNull())
		return false;

	value = TDataStd_Shape::Get(subLabel);
	if(value.IsNull())
		return false;

	return true;
}

void LabelTools::SetChild(TDF_Label& Label, UINT nIndex, const Handle(TColgp_HArray1OfPnt)& value)
{
	for(int i = value->Lower(); i <= value->Upper(); ++i)
	{
		SetChild(Label, nIndex+i-1, value->Value(i));
	}

}

bool LabelTools::GetChild(const TDF_Label& Label,UINT nIndex, Handle(TColgp_HArray1OfPnt)& value, 
						  TDF_Label& subLabel, int nUpper)
{
	value = new  TColgp_HArray1OfPnt(1, nUpper);
	gp_Pnt point;

	for(int i=1; i<= nUpper; ++i)
	{
		if(!GetChild(Label, nIndex+i-1, point, subLabel))
			return false;

		value->SetValue(i, point);
	}

	return true;
}

void LabelTools::SetChild(TDF_Label& Label, UINT nIndex, Standard_Integer value)
{
	TDataStd_Integer::Set(Label.FindChild(nIndex), value);
}
bool LabelTools::GetChild(const TDF_Label& Label,UINT nIndex, Standard_Integer& value, TDF_Label& subLabel)
{
	subLabel = Label.FindChild(nIndex, Standard_False);
	if(subLabel.IsNull())
		return false;

	Handle(TDataStd_Integer) TSR;
	if (!subLabel.FindAttribute(TDataStd_Integer::GetID(), TSR )) 
		return false;

	value = TSR->Get();
	return true;
}

bool LabelTools::UpdateChild(const TDF_Label& Label,UINT nIndex,gp_Pnt& point,TFunction_Logbook& log)
{
	return true;
}

bool LabelTools::UpdateChild(const TDF_Label& Label,UINT nIndex,int nPointCount,TFunction_Logbook& log)
{
	return true;
}

bool LabelTools::UpdateChild(const TDF_Label& Label,UINT nIndex,TopoDS_Shape pShape, TFunction_Logbook& log)
{
	return true;
}

bool LabelTools::UpdateChild(const TDF_Label& label,UINT nIndex,gp_Dir& di,TFunction_Logbook& log)
{	return true;
}

bool LabelTools::v()
{
	return true;
}